package com.djl.util;

import java.lang.reflect.Field;
import java.lang.reflect.Type;

public class CommonUtil {

    // 将字符串首字母小写
    public static String lowerFirstCase(String simpleName) {
        char[] chars = simpleName.toCharArray();
        chars[0] += 32;   // ASCII    a-A   b-B   c-C
        return String.valueOf(chars);
    }

    /**
     * 获取返回值类型
     */
    public static String getReturnValueType(Field field){
        Type genericType = field.getGenericType();
        String typeName = genericType.getTypeName();
        return lowerFirstCase(typeName.split("\\.")[typeName.split("\\.").length-1]);
    }

    // 把String类型转变成其他类型
    public static Object string2Other(String clientValue, Class<?> parameterType){
        if(String.class==parameterType){
            return clientValue;
        }
        if(Integer.class==parameterType){
            return Integer.valueOf(clientValue);
        }else if(Double.class==parameterType){
            return Double.valueOf(clientValue);
        }else{
            return null;
        }
    }
}
